"""
    @Author  ：思念 
    @File    ：6.标签等待.py
    @Date    ：2025/3/26 14:57 
"""
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
# 等待页面标签加载
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# 定义浏览器对象
browser = webdriver.Chrome()
browser.maximize_window()
# 创建浏览器等待对象 默认等待时间10秒
wait = WebDriverWait(browser, 10)
# 网址访问
browser.get("http://www.jd.com")

# 获取标签 presence_of_element_located: 需要传递一个元组参数
input_search = wait.until(EC.presence_of_element_located((By.ID, 'key'))).send_keys('汉服')
# time.sleep(5)
# 判断是否有阻挡元素
try:
    img_handle = wait.until(EC.presence_of_element_located((By.XPATH, '//img[@class="umc-advert-link"]')))
    print("有阻挡元素:", img_handle)
    img_close_button = wait.until(EC.element_to_be_clickable((By.XPATH, '//img[@class="umc-equity-close"]')))
    print("获取到关闭按钮:", img_close_button)
    img_close_button.click()
    print("有阻挡元素,正在关闭...")
except Exception:
    print("无阻挡元素")
    pass

button_search = wait.until(EC.element_to_be_clickable((By.XPATH, '//button[@class="button"]'))).click()
time.sleep(5)
browser.quit()
